import { AxiosRequestConfig, AxiosResponse } from 'axios';

export type CodeType = string|number;


export interface SetErrorParams {
    code: number;
    msg: string;
}

export interface BdjfResponse<T> {
    [key: string]: any;
    code: CodeType;
    msg?: string;
    data?: T;
    success: boolean;
    isLoading: boolean;
    _originalData_?: AxiosResponse<T>;
    update: (newRes: BdjfResponse<T>) => BdjfResponse<T>;
    setLoading: (data?: T) => BdjfResponse<T>;
    setError: (err: SetErrorParams|string) => BdjfResponse<T>;
    setSuccess: (data?: T) => BdjfResponse<T>;
}

export interface AttributeMapping {
    code: string;
    msg: string;
    data: string;
}


export type CacheKeyType = string[]|'all';

export interface CacheOption {
    bindCacheKey?: CacheKeyType;
    excludeCacheKey?: string[];
    conflictCacheUrl?: BdjfRequest[];
}

export interface RequestOption extends AxiosRequestConfig {
    // 重写了 AxiosRequestConfig 中的 url，将其改为必须值
    url: string;
    needCache?: boolean;
    loadingText?: string;
    hideLoading?: boolean;
    hideError?: boolean;
    hideWarn?: boolean;
    attributeMapping?: AttributeMapping;
    rejectError?: boolean;
    extraData?: any;
}

export interface BdjfRequest {
    requestOption: RequestOption;
    cacheOption?: CacheOption;
    closeLoading: () => BdjfRequest;
    openLoading: () => BdjfRequest;
    setOption: (newOption: RequestOption) => BdjfRequest;
    setCacheOption: (newOption: CacheOption) => BdjfRequest;
    conflictCacheUrl: () => string[];
}

export interface HttpConfig {

    showLoading: (request: BdjfRequest) => void;

    hideLoading: <T>(request: BdjfRequest) => void;

    showWarn: <T>(res: BdjfResponse<T>, request: BdjfRequest) => void;

    showError: <T>(res: BdjfResponse<T>, request: BdjfRequest) => void;

    successCode: CodeType;

    attributeMapping: AttributeMapping;

    openLog?: boolean;

}
